package stdio.costa.imoca.api;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import stdio.costa.imoca.converter.Activities2ActivityListWrapper;
import stdio.costa.imoca.converter.Activity2DTO;
import stdio.costa.imoca.dto.ActivityListDTO;
import stdio.costa.imoca.dto.ActivityWithPostcardbackDTO;
import stdio.costa.imoca.model.Activity;
import stdio.costa.imoca.model.Image;
import stdio.costa.imoca.model.PostcardBack;
import stdio.costa.imoca.spi.ActivitySpi;
import stdio.costa.imoca.spi.PostcardBackSpi;
import stdio.costa.imoca.utils.TimeUtil;

import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataParam;

@Path("/activity")
@Component
@Scope("request")
public class ActivityApi extends BaseApi {

	@Autowired
	private ActivitySpi activitySpi;

	@Autowired
	private PostcardBackSpi postcardBackSpi;

	@Autowired
	private ImageApi imageApi;

	/*
	 * get activity list
	 */
	@Path("/get_list")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public ActivityListDTO getList(
			@QueryParam("accessToken") String accessTokenCode) {

		List<Activity> activities = this.activitySpi.findAllActivities();

		ActivityListDTO wrapper = new ActivityListDTO();

		if (activities != null) {
			wrapper = Activities2ActivityListWrapper.convert(activities);
		}

		return wrapper;

	}

	/*
	 * get one activity info
	 */
	@Path("/get")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Activity get(@QueryParam("accessToken") String accessTokenCode,
			@QueryParam("activity_uuid") String activityUuid) {

		Activity activity = this.activitySpi.findActivityByUuid(activityUuid);

		return activity;

	}

	@Path("/get_postcardbacks")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public ActivityWithPostcardbackDTO getPostcardbacks(
			@QueryParam("accessToken") String accessTokenCode,
			@QueryParam("activity_uuid") String activityUuid) {

		Activity activity = this.activitySpi.findActivityByUuid(activityUuid);

		if (activity != null) {

			activity.setPostcardBacks(this.postcardBackSpi
					.findPostcardBacksByActivity(activity));
		}

		ActivityWithPostcardbackDTO dto = Activity2DTO.convert(activity);

		return dto;
	}

	/*
	 * add one activity
	 */

	@Path("/create")
	@POST
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public Activity create(@QueryParam("accessToken") String accessTokenCode,
			@QueryParam("name") String name,
			@QueryParam("startTime") String startTimeStr,
			@QueryParam("endTime") String endTimeStr,
			@QueryParam("description") String description) throws Exception {

		DateFormat startTimeDateFormat = new java.text.SimpleDateFormat(
				TimeUtil.TIME_FORMAT);
		DateFormat endTimeDateFormat = new java.text.SimpleDateFormat(
				TimeUtil.TIME_FORMAT);
		Date startTime = startTimeDateFormat.parse(startTimeStr);
		Date endTime = endTimeDateFormat.parse(endTimeStr);

		Activity activity = new Activity(name, startTime, endTime, description);

		this.activitySpi.save(activity);

		return activity;
	}

	@Path("/add_image")
	@POST
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Transactional
	public Activity addImage(
			@FormDataParam("accessToken") String accessTokenCode,
			@FormDataParam("activityUuid") String activityUuid,
			@FormDataParam("photo") FormDataBodyPart photoPart)
			throws Exception {

		Image photo = imageApi.create(photoPart);

		Activity activity = this.activitySpi.findActivityByUuid(activityUuid);

		PostcardBack back = new PostcardBack();

		back.setUrl(photo.getMediumUrl());

		if (activity == null) {
		}

		this.activitySpi.save(activity);

		back.setActivity(activity);

		this.postcardBackSpi.save(back);

		return activity;

	}

	/*
	 * edit one activity
	 */
	public Activity edit() {
		return null;
	}

	/*
	 * delete one activity
	 */
	public Activity delete() {
		return null;
	}
}
